package updater;

import java.text.ParseException;

public class MapShareVersionNumber implements Comparable<MapShareVersionNumber>{
	public int MAJOR;
	public int MINOR;
	public int ISSUE;
	public int BUILD;
	
	public MapShareVersionNumber(String version) throws ParseException {
		parse(version);
	}
	
	public void parse(String version) throws ParseException{
		String[] temp = version.split("\\D");
		if(temp.length == 4){
			try {
				MAJOR = Integer.parseInt(temp[0]);
				MINOR = Integer.parseInt(temp[1]);
				ISSUE = Integer.parseInt(temp[2]);
				BUILD = Integer.parseInt(temp[3]);
			} catch (NumberFormatException e){
				throw new ParseException(version + " could not be parsed.  Details: "+ e.getMessage(), 0);
			}
		} else {
			throw new ParseException(version + " could not be parsed.  Expected format: MAJOR.MINOR.ISSUE.BUILD", 0);
		}
	}

	@Override
	public int compareTo(MapShareVersionNumber o) {
		int result = 0;
		result = Integer.compare(MAJOR, o.MAJOR);
		if(result == 0)
			result = Integer.compare(MINOR, o.MINOR);
		if(result == 0)
			result = Integer.compare(ISSUE, o.ISSUE);
		if(result == 0)
			result = Integer.compare(BUILD, o.BUILD);
		return result;
	}
	
	public String toString(){ 
		return "" + MAJOR + "." + MINOR + "." + ISSUE + "." + BUILD; 
	}
}
